<?php
require __DIR__ . '/../vendor/autoload.php';

function getVideoUrlsByMediaNames($csvFile, $mediaNames) {
    $videoUrls = array();
    if (($handle = fopen($csvFile, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            // 将CSV数据转换为UTF-8编码
            $data = array_map(function($value) {
                return iconv('gbk', 'utf-8', $value); // 假设CSV文件是gbk编码
//                return mb_convert_encoding($value, 'UTF-8', 'auto');
            }, $data);
            $str1 = trim($data[0]);
            if (strpos($data[12], 'mp4') !== false){
                foreach ($mediaNames as $item){
                    $steamKey = $item[3];
                    $startTime = strtotime($item[1]) - 60;
                    $endTime = strtotime($item[2]) + 60;
                    $dataTime = strtotime($data[1]);
                    if (strpos($str1, $steamKey) !== false){
                        if ($dataTime >= $startTime && $dataTime <= $endTime){
                            $videoUrls[$steamKey][$item[0]][] = $data[12];
                        }
                    }
                }
            }
        }
        fclose($handle);
    }
    return $videoUrls;
}

// 使用方法
$csvFile = './主播账户354928280的腾讯云直播记录.csv';
$playbackAddressFile = './媒资管理数据列表-2024-05-28.csv';
$mediaNames = [];
function isChinese($str) {
    return preg_match("/[\x{4e00}-\x{9fa5}]+/u", $str);
}
if (($handle = fopen($csvFile, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        // 将CSV数据转换为UTF-8编码
        $data = array_map(function($value) {
            return iconv('gbk', 'utf-8', $value); // 假设CSV文件是gbk编码
//            return mb_convert_encoding($value, 'UTF-8', 'auto');
        }, $data);
        if (!isChinese($data[2])){
            $arr = [
                $data[0],$data[3],$data[4],$data[2]
            ];
            $mediaNames[] = $arr;
        }
    }
    fclose($handle);
}
//echo json_encode($mediaNames);
//return [];
$videoUrls = getVideoUrlsByMediaNames($playbackAddressFile, $mediaNames);
//$jsonFile = file_get_contents('./1.json');
//$videoUrls = json_decode($jsonFile,true);

if (($handle = fopen($csvFile, "r")) !== FALSE) {
    // 打开CSV文件
    $fp = fopen('output.csv', 'w');
    $bom = "\xEF\xBB\xBF"; // UTF-8 BOM
    fwrite($fp, $bom);
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        // 将CSV数据转换为UTF-8编码
        $data = array_map(function($value) {
            return iconv('gbk', 'utf-8', $value); // 假设CSV文件是gbk编码
        }, $data);
        if (isset($videoUrls[$data[2]])){
            $line = $videoUrls[$data[2]];
            foreach ( $line as $lk => $item){
                if ($lk == $data[0]){
                    foreach ($item as $ik => $iv){
                        $data[5+$ik] = $iv;
                    }
                }
            }
        }
        // 写入CSV数据
        fputcsv($fp, $data);
    }
    fclose($handle);
    // 关闭CSV文件
    fclose($fp);
}
echo json_encode($videoUrls);
//echo json_encode($replayLines);